package com.lucky.contract.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lucky.contract.entity.SysUser;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginFilter extends HandlerInterceptorAdapter {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String url5 = request.getRequestURI();
		if(isLogin(request) || url5.contains("assets")) {
			return true;
		} else {
			if (handler instanceof HandlerMethod) {
				if(url5.startsWith("/login/logout") || url5.startsWith("/login/login") || url5.startsWith("/login/page")) {
					return true;
				}
			}
			response.sendRedirect("/login/page");
			return false;
		}
	}
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {    
		if(modelAndView != null) {
			SysUser loginUser = null;
			try {
				loginUser = (SysUser)request.getSession().getAttribute("loginPersonId");
			} catch (Exception e) {
			}
			modelAndView.addObject("loginUser", loginUser);
		}
	}
	public Boolean isLogin(HttpServletRequest request) {
		Object loginId = request.getSession().getAttribute("loginPersonId");
		if(loginId != null) {
			return true;
		} else {
			return false;
		}
	}
}
